/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"67579A21-B6B0-4918-BD38-3227C3E92EB7"}
 */
function onDataChangeModelo(oldValue, newValue, event) {
	lista_precios_cab_to_lista_precios_det.deleteAllRecords()
	
	if (newValue != null)
	{
		var $query = 'SELECT p.producto_id, p.observ_lista_precio, p.orden_rubro, p.descripcion, e.precio_venta FROM existencias as "e"' +
  					' join productos as "p" on e.cod_producto = p.cod_producto '+
					' where e.lista_'+ lista_precios_cab_to_lista_precios_mod.lista_base +' = 1'
		var $args 	= null
		var $max 	= -1
		var $server = 'infocom'
		
		var $ds = databaseManager.getDataSetByQuery($server,$query,$args,$max)
		var $cant = $ds.getMaxRowIndex()
		var $dtl = lista_precios_cab_to_lista_precios_det
		for (var i = 1 ; i <= $cant ; i++)
		{
			$ds.rowIndex = i

			$dtl.newRecord()
			$dtl.producto_id 			= $ds[1]
			$dtl.observ_lista_precio	= $ds[2]
			$dtl.orden_rubro			= $ds[3]
			$dtl.descripcion			= $ds[4]
			$dtl.precio 				= $ds[5] * ((100 + lista_precios_cab_to_lista_precios_mod.porc_varia) / 100)
		}
	}
	return true
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} [firstShow] form is shown first time after load
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @properties={typeid:24,uuid:"9DC753A1-C45C-402E-9233-46EB742487AA"}
 */
function onShow(firstShow, event) {
	databaseManager.setAutoSave(false)
	controller.newRecord()
	
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"F46A0524-5BFB-4C69-B60F-18822C2039EC"}
 */
function confirmar(event) {
	if (databaseManager.saveData())
		databaseManager.setAutoSave(true)
	else
	{
		globals.DIALOGS.showWarningDialog('Aviso','Ocurrió un error en la grabación','OK')
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @properties={typeid:24,uuid:"495F7C40-B48B-4B78-B201-EC4A1C202B9D"}
 */
function cancelar(event) {
	databaseManager.revertEditedRecords()
	onShow()
}
